package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.query.DestinationQueryObject;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/destination")
public class DestinationController {

    @Autowired
    private IDestinationService destinationService;


    @RequestMapping("/list")
    public String list(Model model,@ModelAttribute("qo") DestinationQueryObject qo,String parentId){

        Page page = destinationService.list(qo);
        List<Destination> toasts = destinationService.getToasts(parentId);
        model.addAttribute("page",page);
        model.addAttribute("toasts",toasts);

        return "/destination/list";
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Destination destination){
        if(destination.getId() == null){
            destinationService.save(destination);
        }else{
            destinationService.update(destination);
        }
        return JsonResult.success();
    }

    @RequestMapping("/getDestByDeep")
    @ResponseBody
    public Object getDestByDeep(){
        List<Destination> list = destinationService.findAll();

        return list;
    }


    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(String id){
        destinationService.delete(id);

        return JsonResult.success();
    }


}
